package com.foodorder.controller;

import com.foodorder.dto.ApiResponse;
import com.foodorder.entity.StoreFollow;
import com.foodorder.service.StoreFollowService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店铺关注控制器
 */
@RestController
@RequestMapping("/store-follows")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class StoreFollowController {
    
    private final StoreFollowService storeFollowService;
    
    /**
     * 关注店铺
     */
    @PostMapping("/follow")
    public ApiResponse<String> followStore(@RequestParam Long userId, @RequestParam Long storeId) {
        boolean success = storeFollowService.followStore(userId, storeId);
        if (success) {
            return ApiResponse.success("关注成功");
        } else {
            return ApiResponse.error("已关注该店铺");
        }
    }
    
    /**
     * 取消关注店铺
     */
    @PostMapping("/unfollow")
    public ApiResponse<String> unfollowStore(@RequestParam Long userId, @RequestParam Long storeId) {
        boolean success = storeFollowService.unfollowStore(userId, storeId);
        if (success) {
            return ApiResponse.success("取消关注成功");
        } else {
            return ApiResponse.error("未关注该店铺");
        }
    }
    
    /**
     * 检查是否关注了店铺
     */
    @GetMapping("/check")
    public ApiResponse<Boolean> checkFollowStatus(@RequestParam Long userId, @RequestParam Long storeId) {
        boolean isFollowing = storeFollowService.isFollowing(userId, storeId);
        return ApiResponse.success(isFollowing);
    }
    
    /**
     * 获取用户关注的店铺列表
     */
    @GetMapping("/user/{userId}")
    public ApiResponse<List<StoreFollow>> getUserFollowedStores(@PathVariable Long userId) {
        List<StoreFollow> follows = storeFollowService.getUserFollowedStores(userId);
        return ApiResponse.success(follows);
    }
    
    /**
     * 获取店铺的关注者列表
     */
    @GetMapping("/store/{storeId}")
    public ApiResponse<List<StoreFollow>> getStoreFollowers(@PathVariable Long storeId) {
        List<StoreFollow> followers = storeFollowService.getStoreFollowers(storeId);
        return ApiResponse.success(followers);
    }
    
    /**
     * 获取店铺关注数
     */
    @GetMapping("/store/{storeId}/count")
    public ApiResponse<Long> getStoreFollowCount(@PathVariable Long storeId) {
        long count = storeFollowService.getStoreFollowCount(storeId);
        return ApiResponse.success(count);
    }
    
    /**
     * 获取用户关注的店铺数
     */
    @GetMapping("/user/{userId}/count")
    public ApiResponse<Long> getUserFollowCount(@PathVariable Long userId) {
        long count = storeFollowService.getUserFollowCount(userId);
        return ApiResponse.success(count);
    }
    
    /**
     * 更新通知设置
     */
    @PutMapping("/notification")
    public ApiResponse<String> updateNotificationSetting(
            @RequestParam Long userId, 
            @RequestParam Long storeId, 
            @RequestParam boolean receiveNotifications) {
        boolean success = storeFollowService.updateNotificationSetting(userId, storeId, receiveNotifications);
        if (success) {
            return ApiResponse.success("通知设置更新成功");
        } else {
            return ApiResponse.error("更新失败，请检查关注状态");
        }
    }
} 